package com.test;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.core.db.condition.Condition;
import com.core.db.execute.Dao;
import com.core.db.execute.Transaction;
import com.core.db.execute.Transaction.Worker;
import com.core.mvc.Require;
import com.core.mvc.annotation.Action;
import com.core.mvc.annotation.Service;
import com.test.bean.Website;

@Service( "/DbService" )
public class DBService
{
	@Action( "show" )
	public void show( Require require )
	{
		try
		{
			require.getResp().setContentType( "text/html;charset=UTF-8" );
			PrintWriter out = require.getResp().getWriter();
			String title = "Servlet Mysql 测试 - 菜鸟教程";
			String docType = "<!DOCTYPE html>\n";
			out.println( docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"
							+ "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title
							+ "</h1>\n" );
			// 执行 SQL 查询
			Condition cond = new Condition();
//			cond.where().and( "name", "=", "Google" ).or( "url", "=", "www.yint.net.cn" );
			cond.order().desc( "name" ).asc( "id" );
			List<Website> websites= Dao.query( Website.class, cond );
			for(Website website:websites)
			{
				// 输出数据
				out.println( "ID: " + website.getId() );
				out.println( ", 站点名称: " + website.getName() );
				out.println( ", 站点 URL: " + website.getUrl() );
				out.println( "<br />" );
			}
			out.println( "</body></html>" );
		}
		catch ( Exception e )
		{
			// 处理 Class.forName 错误
			e.printStackTrace();
		}
	}

	@Action( "insert" )
	public void insert( Require require )
	{
		try
		{
			// 执行 SQL 查询
			Website website=new Website();
			website.setName( String.valueOf( System.currentTimeMillis()));
			website.setUrl( "www.yint.net.cn" );
			boolean rs = Dao.insert( website );
		}
		catch ( Exception e )
		{
			// 处理 Class.forName 错误
			e.printStackTrace();
		}
		show( require );
	}

	@Action( "delect" )
	public void delect( Require require )
	{
		try
		{
			// 执行 SQL 查询
			Condition cond = new Condition();
			cond.where().and( "url", "=", "www.yint.net.cn" ).or( "id", ">=", 6 );
			boolean rs = Dao.delect( Website.class, cond );
		}
		catch ( Exception e )
		{
			// 处理 Class.forName 错误
			e.printStackTrace();
		}
		show( require );
	}

	@Action( "update" )
	public void update( Require require )
	{
		try
		{
			// 执行 SQL 查询
			Condition cond = new Condition();
			Website website=new Website();
			website.setName("name-1");
			cond.where().and( "url", "=", "www.yint.net.cn" );
			boolean rs = Dao.update( website, cond );
		}
		catch ( Exception e )
		{
			// 处理 Class.forName 错误
			e.printStackTrace();
		}
		show( require );
	}

	@Action( "trans" )
	public void trans( Require require )
	{
		new Transaction( new Worker()
		{

			@Override
			public void exec( Transaction trans )
			{
				try
				{
					// 执行 SQL 查询
					Website website=new Website();
					website.setName( String.valueOf( System.currentTimeMillis()));
					website.setUrl( "www.yint.net.cn" );
					trans.insert( website );
					// 执行 SQL 查询
					Condition cond = new Condition();
					website=new Website();
					website.setName("name-1");
					cond.where().and( "url", "=", "www.yint.net.cn" );
					trans.update( website, cond );
//					trans.raw( "SELECT * FROM website" );
				}
				catch ( SQLException e )
				{
					e.printStackTrace();
				}
				System.out.println( "exec over" );
			}
		} ).exec();
		System.out.println( "trans over" );
		show( require );
	}
}
